<?php

namespace App\Http\Controllers\admin;

use App\Http\Model\Member;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class MemberController extends AdminPlatformController
{
    /**
     * 会员管理模块展示
     */
    public function member_list(){
        $data = Member::orderBy('user_id','desc')->paginate(2);
        return view('admin.member.memberList',compact('data'));
    }
    /**
     * 会员管理添加模块
     */
    public function member_add(){
        return view('admin.member.memberAdd');
    }

    /**
     * 提交会员管理添加
     */
    public function postmember_add(){
        $input = Input::except('_token');
        $input['reg_time']=time();
        $salt  = rand(1000,9999);
        $input['salt'] = $salt;
        $input['password']=md5($salt.$input['password']);
        $rlu = [
            "user_name" => "required|max:20|min:3",
            "password" => "required|max:50|min:6",
            "age" => "required|max:2|min:1",
            "phone" => "required|numeric|",
            "email" => "required|email",
            "address" => "required"
        ];
        $massg = [
            "user_name.required" =>'用户名不能为空',
            "user_name.max:20" =>'用户名最多20个字符',
            "user_name.min:3" =>'用户名最少三个字符',
            "password.required" =>'密码不能为空',
            "password.max:50" =>'密码最多50位',
            "password.min:6" =>'密码最少6',
            "age.required" =>'年龄不能为空',
            "age.max:2" =>'年龄最大100',
            "age.min:1" =>'年龄最小18',
            "phone.required" =>'电话号码不能为空',
            "phone.max:2" =>'电话号码必须11位',
            "phone.min:1" =>'电话号码必须11位',
            "phone.numeric" =>'电话号码必须为数字',
            "email.required" =>'email不能为空',
            "email.email" =>'email格式不对',
            "address.required" =>'住址不能为空',
        ];

        $validate = Validator::make($input, $rlu, $massg);
        if ($validate->passes()) {

            $rs = Member::create($input);
            if ($rs) {
                return redirect('manager/member');
            } else {
                return back()->with('errors', '添加失败未知错误请稍后再试');
            }
        } else {
            return back()->withErrors($validate);
        }
    }

    /**
     * 删除会员
     * @param $id int 会员主键
     */
    public function member_del($id){
        $rs = Member::where('user_id',$id)->delete();
        if($rs){
            return redirect('manager/member');
        }else{
            return back()->with('errors','删除失败稍后再试');
        }
    }
    /**
     * 编辑会员模板
     * @param $id int 会员主键
     */
    public function member_edit($id){
        $data = Member::find($id);
        return view('admin.member.memberEdit',compact('data'));
    }
    /**
     * 提交会员编辑
     * @param $id int 会员主键
     */
    public function postmember_edit($id){
        $input = Input::except('_token');
        $input['reg_time']=time();
        $salt  = rand(1000,9999);
        $input['salt'] = $salt;
        $input['password']=md5($salt.$input['password']);
        $rlu = [
            "user_name" => "required|max:20|min:3",
            "password" => "required|max:50|min:6",
            "age" => "required|max:2|min:1",
            "phone" => "required|numeric|",
            "email" => "required|email",
            "address" => "required"
        ];
        $massg = [
            "user_name.required" =>'用户名不能为空',
            "user_name.max:20" =>'用户名最多20个字符',
            "user_name.min:3" =>'用户名最少三个字符',
            "password.required" =>'密码不能为空',
            "password.max:50" =>'密码最多50位',
            "password.min:6" =>'密码最少6',
            "age.required" =>'年龄不能为空',
            "age.max:2" =>'年龄最大100',
            "age.min:1" =>'年龄最小18',
            "phone.required" =>'电话号码不能为空',
            "phone.max:2" =>'电话号码必须11位',
            "phone.min:1" =>'电话号码必须11位',
            "phone.numeric" =>'电话号码必须为数字',
            "email.required" =>'email不能为空',
            "email.email" =>'email格式不对',
            "address.required" =>'住址不能为空',
        ];

        $validate = Validator::make($input, $rlu, $massg);
        if ($validate->passes()) {
            $member = new member();
            $rs = $member->where('user_id',$id)->update($input);
            if ($rs) {
                return redirect('manager/member');
            } else {
                return back()->with('errors', '编辑失败未知错误请稍后再试');
            }
        } else {
            return back()->withErrors($validate);
        }
    }



}
